import flash.events.Event;
import flash.display.Sprite;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Matrix;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;

import haxe.FastList;

import Images;
import GameData;

class UpgradeLevel
{
  public var Cost:Int;
  public var Level:Int;
  public var Effect:Float;
  public function new(inCost:Int,inLevel:Int,inEffect:Float)
  {
    Cost = inCost;
    Level = inLevel;
    Effect = inEffect;
  }
}
//Must be ordered and distiginable.
class UpgradeList
{
  public var upgradeLevelStats:FastList<UpgradeLevel>;
  public var currentLevel:Int;
  public function new()
  {
    upgradeLevelStats = new FastList<UpgradeLevel>();
    currentLevel = 0;
  }
  public function getLevel(inEffect:Float):Int
  {
    var retVal:Int = -1;
    if(upgradeLevelStats == null || upgradeLevelStats.first() == null)
      return retVal;
    for(item in upgradeLevelStats.iterator())
      {
      if(item.Effect == inEffect)
	{retVal = item.Level;}
      }
     return retVal;
  }
  public function getNextLevelCost(inLevel:Int):String
  {
    var retVal:String = "Maxed";
    if(upgradeLevelStats == null || upgradeLevelStats.first() == null)
      return retVal;
    var nextItem:Bool=false;
    for(item in upgradeLevelStats.iterator())
      {
	if(nextItem)
	{
	  retVal = Std.string(item.Cost);
	  nextItem = false;
	  break;
	}
	if(item.Level == inLevel)
	  {nextItem = true;}
      }
     return retVal;
  }

  public function getNextLevelEffect(inLevel:Int):Float
  {
    var retVal:Float = -1;
    if(upgradeLevelStats == null || upgradeLevelStats.first() == null)
      return retVal;
    var nextItem:Bool=false;
    for(item in upgradeLevelStats.iterator())
      {
	if(nextItem)
	{
	  retVal = item.Effect;
	  nextItem = false;
	  break;
	}
	if(item.Level == inLevel)
	  {nextItem = true;}
      }
     return retVal;
  }
  public function isMaxed(inLevel:Int):Bool
  {
    var retVal:Bool = false;
    if(getNextLevelEffect(inLevel) <= -1)
      {retVal = true;}
      
    return retVal;
  }
}

class Upgrades
{
  public var ShipSizeUpgrade:UpgradeList;
  public var HealthUpgrade:UpgradeList;
  public var PowerUpAppearanceUpgrade:UpgradeList;
  public var InvincibilityLengthUpgrade:UpgradeList;
  public var PowerUpFadeRateUpgrade:UpgradeList;
  public var MoneyMultiplierUpgrade:UpgradeList;
  public var ShotsFiredRateUpgrade:UpgradeList;
  public function new(inDifficulty:Int)
  {
    
    ShipSizeUpgrade = new UpgradeList();
    //Cost,Level,Size
    //Highest on Top
    ShipSizeUpgrade.upgradeLevelStats.add(new UpgradeLevel(1000,5,7));
    ShipSizeUpgrade.upgradeLevelStats.add(new UpgradeLevel(500,4,10));
    ShipSizeUpgrade.upgradeLevelStats.add(new UpgradeLevel(100,3,15));
    ShipSizeUpgrade.upgradeLevelStats.add(new UpgradeLevel(10,2,17));
    ShipSizeUpgrade.upgradeLevelStats.add(new UpgradeLevel(1,1,20));
    ShipSizeUpgrade.upgradeLevelStats.add(new UpgradeLevel(0,0,40));

    HealthUpgrade = new UpgradeList();
    HealthUpgrade.upgradeLevelStats.add(new UpgradeLevel(1000,5,100));
    HealthUpgrade.upgradeLevelStats.add(new UpgradeLevel(250,4,75));
    HealthUpgrade.upgradeLevelStats.add(new UpgradeLevel(100,3,50));
    HealthUpgrade.upgradeLevelStats.add(new UpgradeLevel(20,2,20));
    HealthUpgrade.upgradeLevelStats.add(new UpgradeLevel(5,1,15));
    HealthUpgrade.upgradeLevelStats.add(new UpgradeLevel(0,0,10));
    
    //Cost,Level,Size
    PowerUpAppearanceUpgrade = new UpgradeList();
//     PowerUpAppearanceUpgrade.upgradeLevelStats.add(new UpgradeLevel(100,5,5));
//     PowerUpAppearanceUpgrade.upgradeLevelStats.add(new UpgradeLevel(50,4,4));
    PowerUpAppearanceUpgrade.upgradeLevelStats.add(new UpgradeLevel(1000,3,3));
    PowerUpAppearanceUpgrade.upgradeLevelStats.add(new UpgradeLevel(500,2,2));
    PowerUpAppearanceUpgrade.upgradeLevelStats.add(new UpgradeLevel(100,1,1));
    PowerUpAppearanceUpgrade.upgradeLevelStats.add(new UpgradeLevel(0,0,0));
    
    InvincibilityLengthUpgrade = new UpgradeList();
    if(inDifficulty < 2)
    {
      InvincibilityLengthUpgrade.upgradeLevelStats.add(new UpgradeLevel(500,2,200));
      InvincibilityLengthUpgrade.upgradeLevelStats.add(new UpgradeLevel(100,1,150));
    }
    InvincibilityLengthUpgrade.upgradeLevelStats.add(new UpgradeLevel(0,0,100));

    PowerUpFadeRateUpgrade = new UpgradeList();
    PowerUpFadeRateUpgrade.upgradeLevelStats.add(new UpgradeLevel(1000,4,400));
    PowerUpFadeRateUpgrade.upgradeLevelStats.add(new UpgradeLevel(500,3,300));
    PowerUpFadeRateUpgrade.upgradeLevelStats.add(new UpgradeLevel(100,2,200));
    PowerUpFadeRateUpgrade.upgradeLevelStats.add(new UpgradeLevel(10,1,150));
    PowerUpFadeRateUpgrade.upgradeLevelStats.add(new UpgradeLevel(0,0,100));
  
    MoneyMultiplierUpgrade = new UpgradeList();
    MoneyMultiplierUpgrade.upgradeLevelStats.add(new UpgradeLevel(1000,4,4));
    MoneyMultiplierUpgrade.upgradeLevelStats.add(new UpgradeLevel(250,3,3.5));
    MoneyMultiplierUpgrade.upgradeLevelStats.add(new UpgradeLevel(50,2,3));
    MoneyMultiplierUpgrade.upgradeLevelStats.add(new UpgradeLevel(10,1,2));
    MoneyMultiplierUpgrade.upgradeLevelStats.add(new UpgradeLevel(0,0,1));

    ShotsFiredRateUpgrade = new UpgradeList();
    if(inDifficulty < 3)
    {
    ShotsFiredRateUpgrade.upgradeLevelStats.add(new UpgradeLevel(1000,1,2));
    }
    ShotsFiredRateUpgrade.upgradeLevelStats.add(new UpgradeLevel(0,0,1));
  }
}

class UpgradePageEvent extends Event{
 
	public function new(customEventString:String){

	super(customEventString, true, false);

    }
}

class UpgradePage extends Sprite
{
  private var BackButton:Sprite;
  private var CurrentBackground: Bitmap;
  private var Text:TextField;//Title
  private var Money:TextField;
  private var SmallerShip:CustomSprite;
  private var SmallerShipSize:TextField;
  private var SmallerShipMaxed:TextField;

  private var Health:CustomSprite;
  private var HealthAmount:TextField;
  private var HealthMaxed:TextField;

  private var PowerUpAppearance:CustomSprite;
  private var PowerUpAppearanceAmount:TextField;
  private var PowerUpAppearanceMaxed:TextField;

  private var InvincibilityLength:CustomSprite;
  private var InvincibilityLengthLevel:TextField;
  private var InvincibilityLengthMaxed:TextField;

  private var PowerUpFadeRate:CustomSprite;
  private var PowerUpFadeRateLevel:TextField;
  private var PowerUpFadeRateMaxed:TextField;

  private var MoneyMultiplier:CustomSprite;
  private var MoneyMultiplierLevel:TextField;
  private var MoneyMultiplierMaxed:TextField;

  private var ShotsFiredRate:CustomSprite;
  private var ShotsFiredRateLevel:TextField;
  private var ShotsFiredRateMaxed:TextField;

  public var gameData:GameData;
  public var UpgradesInfo:Upgrades;
  public function new()
  {
    super();

    //Resize Image
    var textBackgroundImage:CircleBackground = new CircleBackground();
    var scaledWidth:Float = (Constants.flashWidth/textBackgroundImage.width);
    var scaledHeight:Float = (Constants.flashHeight/textBackgroundImage.height);

    var matrix:Matrix = new Matrix();
    matrix.scale(scaledWidth, scaledHeight);

    var smallBMD:BitmapData = new BitmapData(Std.int(textBackgroundImage.width * scaledWidth), Std.int(textBackgroundImage.height * scaledHeight),true, 0x000000);

    smallBMD.draw(textBackgroundImage, matrix, null, null, null, true);

    CurrentBackground = new Bitmap(smallBMD);//, PixelSnapping.NEVER, true);
    //End Resize
    addChild(CurrentBackground);

    var textEllipse:CustomSprite = new CustomSprite();
    textEllipse.graphics.lineStyle(1);
    textEllipse.graphics.beginFill(0xFFFFFF);
    textEllipse.graphics.drawEllipse(0, 0,120,50);
    textEllipse.graphics.endFill();

    addChild(textEllipse);
    textEllipse.x=220;
    textEllipse.y=05;

    Text = new TextField();

    Text.x = 230; Text.y = 10;
    Text.width = 440; Text.height = 420;
    addChild(Text);
    Text.multiline = true;
    var myFormat:TextFormat = new TextFormat();
    myFormat.size = 25;
    //myFormat.align = TextFormatAlign.CENTER;
    Text.defaultTextFormat = myFormat;
    
    Text.htmlText = "Upgrades";
    Text.mouseEnabled = false;
    Text.selectable = false;
    //Text.y = ((150 - Text.height)/2);
    Text.wordWrap = true;

  
    Money = new TextField();
    addChild(Money);
    Money.x = 420;
    Money.y = 13;
    Money.background = true;
    Money.backgroundColor = 0xFFFFFF;
    Money.autoSize = TextFieldAutoSize.LEFT;


    SmallerShip = new CustomSprite();
    SmallerShip.graphics.lineStyle(1);
    SmallerShip.graphics.beginFill(0xFFFFFF);
    SmallerShip.graphics.drawCircle(0, 0,40);
    SmallerShip.graphics.endFill();  

    SmallerShip.isHover = true;
    SmallerShip.HoverText = "<u>Smaller Ship - $1</u>\nThis will decrease the Size of your ship";
    SmallerShip.buttonMode = true;
    SmallerShip.addEventListener(MouseEvent.CLICK, onSmallerShipClick);
    SmallerShip.x=80;
    SmallerShip.y=100;
    
    var Circle:Shape = new Shape();
    Circle.graphics.lineStyle(1, 0xFFFFFF);
    Circle.graphics.beginFill(0x000000);
    Circle.graphics.drawCircle(0, 0,20);
    Circle.graphics.endFill();
    SmallerShip.addChild(Circle);
    Circle.x=-10;
    Circle.y=-7;
    Circle = new Shape();
    Circle.graphics.lineStyle(2, 0xFFFFFF);
    Circle.graphics.beginFill(0x000000);
    Circle.graphics.drawCircle(0, 0,10);
    Circle.graphics.endFill();
    SmallerShip.addChild(Circle);
    Circle.x= 5;
    Circle.y = 4;

    Circle = new Shape();
    Circle.graphics.lineStyle(2, 0xFFFFFF);
    Circle.graphics.beginFill(0x000000);
    Circle.graphics.drawCircle(0, 0,5);
    Circle.graphics.endFill();
    SmallerShip.addChild(Circle);
    Circle.x = 14;
    Circle.y = 9;

    SmallerShipSize = new TextField();
    SmallerShip.addChild(SmallerShipSize);
    SmallerShipSize.x = -25;SmallerShipSize.y=15;
    SmallerShipSize.mouseEnabled = false;
    SmallerShipSize.selectable = false;
    SmallerShipSize.autoSize = TextFieldAutoSize.LEFT;

    SmallerShipMaxed = new TextField();
    SmallerShip.addChild(SmallerShipMaxed);
    SmallerShipMaxed.x = -39;SmallerShipMaxed.y=-10;
    SmallerShipMaxed.mouseEnabled = false;
    SmallerShipMaxed.selectable = false;
    SmallerShipMaxed.autoSize = TextFieldAutoSize.LEFT;
    SmallerShipMaxed.background = false;
    SmallerShipMaxed.backgroundColor = 0xFFFFFF;
      
    Health = new CustomSprite();
    Health.graphics.lineStyle(1);
    Health.graphics.beginFill(0xFFFFFF);
    Health.graphics.drawCircle(0, 0,40);
    Health.graphics.endFill();  

    var shape:Shape = new Shape();
    shape.graphics.beginFill(0x000000);
    shape.graphics.drawRect(0, 0,60, 20);
    shape.graphics.endFill();
    Health.addChild(shape);
    shape.x = -35;
    shape.y = -10;

    shape = new Shape();
    shape.graphics.beginFill(0x0000FF);
    shape.graphics.drawRect(0, 0, 68,14);
    shape.graphics.endFill();
    Health.addChild(shape);
    shape.x = -32;
    shape.y = -7;

    Health.isHover = true;
    Health.buttonMode = true;
    Health.addEventListener(MouseEvent.CLICK, onHealthClick);
    Health.x=80;
    Health.y=190;
    HealthAmount = new TextField();
    Health.addChild(HealthAmount);
    HealthAmount.x = -25;HealthAmount.y=15;
    HealthAmount.mouseEnabled = false;
    HealthAmount.selectable = false;
    HealthAmount.autoSize = TextFieldAutoSize.LEFT;

    HealthMaxed = new TextField();
    Health.addChild(HealthMaxed);
    HealthMaxed.x = -39;HealthMaxed.y=-10;
    HealthMaxed.mouseEnabled = false;
    HealthMaxed.selectable = false;
    HealthMaxed.autoSize = TextFieldAutoSize.LEFT;
    HealthMaxed.background = false;
    HealthMaxed.backgroundColor = 0xFFFFFF;


    PowerUpAppearance = new CustomSprite();
    PowerUpAppearance.graphics.lineStyle(1);
    PowerUpAppearance.graphics.beginFill(0xFFFFFF);
    PowerUpAppearance.graphics.drawCircle(0, 0,40);
    PowerUpAppearance.graphics.endFill();  

    shape = new Shape();
    shape.graphics.beginFill(0xFF00FF);
    shape.graphics.lineStyle(1);
    shape.graphics.drawCircle(0,0,10);
    shape.graphics.endFill();
    PowerUpAppearance.addChild(shape);
    shape.x = -16;
    shape.y = -18;

    shape = new Shape();
    shape.graphics.beginFill(0x0000FF);
    shape.graphics.lineStyle(1);
    shape.graphics.drawCircle(0,0,10);
    shape.graphics.endFill();
    PowerUpAppearance.addChild(shape);
    shape.x = 11;
    shape.y = -18;

    shape = new Shape();
    shape.graphics.beginFill(0x00FF00);
    shape.graphics.lineStyle(1);
    shape.graphics.drawCircle(0,0,10);
    shape.graphics.endFill();
    PowerUpAppearance.addChild(shape);
    shape.x = -1;
    shape.y = 5;

    PowerUpAppearance.isHover = true;
    PowerUpAppearance.buttonMode = true;
    PowerUpAppearance.addEventListener(MouseEvent.CLICK, onPowerUpAppearanceClick);
    PowerUpAppearance.x=80;
    PowerUpAppearance.y=280;
    PowerUpAppearanceAmount = new TextField();
    PowerUpAppearance.addChild(PowerUpAppearanceAmount);
    PowerUpAppearanceAmount.x = -25;PowerUpAppearanceAmount.y=15;
    PowerUpAppearanceAmount.mouseEnabled = false;
    PowerUpAppearanceAmount.selectable = false;
    PowerUpAppearanceAmount.autoSize = TextFieldAutoSize.LEFT;

    PowerUpAppearanceMaxed = new TextField();
    PowerUpAppearance.addChild(PowerUpAppearanceMaxed);
    PowerUpAppearanceMaxed.x = -39;PowerUpAppearanceMaxed.y=-10;
    PowerUpAppearanceMaxed.mouseEnabled = false;
    PowerUpAppearanceMaxed.selectable = false;
    PowerUpAppearanceMaxed.autoSize = TextFieldAutoSize.LEFT;
    PowerUpAppearanceMaxed.background = false;
    PowerUpAppearanceMaxed.backgroundColor = 0xFFFFFF;


    InvincibilityLength = new CustomSprite();
    InvincibilityLength.graphics.lineStyle(1);
    InvincibilityLength.graphics.beginFill(0xFFFFFF);
    InvincibilityLength.graphics.drawCircle(0, 0,40);
    InvincibilityLength.graphics.endFill();  

    shape = new Shape();
    shape.graphics.beginFill(0xFF00FF);
    shape.graphics.lineStyle(1);
    shape.graphics.drawCircle(0,0,15);
    shape.graphics.endFill();
    InvincibilityLength.addChild(shape);
    shape.x = 0;
    shape.y = 0;

    InvincibilityLength.isHover = true;
    InvincibilityLength.buttonMode = true;
    InvincibilityLength.addEventListener(MouseEvent.CLICK, onInvincibilityLengthClick);
    InvincibilityLength.x=180;
    InvincibilityLength.y=100;
    InvincibilityLengthLevel = new TextField();
    InvincibilityLength.addChild(InvincibilityLengthLevel);
    InvincibilityLengthLevel.x = -25;InvincibilityLengthLevel.y=15;
    InvincibilityLengthLevel.mouseEnabled = false;
    InvincibilityLengthLevel.selectable = false;
    InvincibilityLengthLevel.autoSize = TextFieldAutoSize.LEFT;

    InvincibilityLengthMaxed = new TextField();
    InvincibilityLength.addChild(InvincibilityLengthMaxed);
    InvincibilityLengthMaxed.x = -39;InvincibilityLengthMaxed.y=-10;
    InvincibilityLengthMaxed.mouseEnabled = false;
    InvincibilityLengthMaxed.selectable = false;
    InvincibilityLengthMaxed.autoSize = TextFieldAutoSize.LEFT;
    InvincibilityLengthMaxed.background = false;
    InvincibilityLengthMaxed.backgroundColor = 0xFFFFFF;

    PowerUpFadeRate = new CustomSprite();
    PowerUpFadeRate.graphics.lineStyle(1);
    PowerUpFadeRate.graphics.beginFill(0xFFFFFF);
    PowerUpFadeRate.graphics.drawCircle(0, 0,40);
    PowerUpFadeRate.graphics.endFill();  

    shape = new Shape();
    shape.graphics.beginFill(0x0000FF);
    shape.graphics.lineStyle(1);
    shape.graphics.drawCircle(0,0,11);
    shape.graphics.endFill();
    PowerUpFadeRate.addChild(shape);
    shape.x = -28;
    shape.y = -3;

    shape = new Shape();
    shape.graphics.beginFill(0x0000FF);
    shape.graphics.lineStyle(1);
    shape.graphics.drawCircle(0,0,11);
    shape.graphics.endFill();
    shape.alpha = 0.6;
    PowerUpFadeRate.addChild(shape);
    shape.x = 0;
    shape.y = -3;

    shape = new Shape();
    shape.graphics.beginFill(0x0000FF);
    shape.graphics.lineStyle(1);
    shape.graphics.drawCircle(0,0,11);
    shape.graphics.endFill();
    shape.alpha = 0.2;
    PowerUpFadeRate.addChild(shape);
    shape.x = 28;
    shape.y = -3;


    PowerUpFadeRate.isHover = true;
    PowerUpFadeRate.buttonMode = true;
    PowerUpFadeRate.addEventListener(MouseEvent.CLICK, onPowerUpFadeRateClick);
    PowerUpFadeRate.x=180;
    PowerUpFadeRate.y=190;
    PowerUpFadeRateLevel = new TextField();
    PowerUpFadeRate.addChild(PowerUpFadeRateLevel);
    PowerUpFadeRateLevel.x = -25;PowerUpFadeRateLevel.y=15;
    PowerUpFadeRateLevel.mouseEnabled = false;
    PowerUpFadeRateLevel.selectable = false;
    PowerUpFadeRateLevel.autoSize = TextFieldAutoSize.LEFT;

    PowerUpFadeRateMaxed = new TextField();
    PowerUpFadeRate.addChild(PowerUpFadeRateMaxed);
    PowerUpFadeRateMaxed.x = -39;PowerUpFadeRateMaxed.y=-10;
    PowerUpFadeRateMaxed.mouseEnabled = false;
    PowerUpFadeRateMaxed.selectable = false;
    PowerUpFadeRateMaxed.autoSize = TextFieldAutoSize.LEFT;
    PowerUpFadeRateMaxed.background = false;
    PowerUpFadeRateMaxed.backgroundColor = 0xFFFFFF;

    MoneyMultiplier = new CustomSprite();
    MoneyMultiplier.graphics.lineStyle(1);
    MoneyMultiplier.graphics.beginFill(0xFFFFFF);
    MoneyMultiplier.graphics.drawCircle(0, 0,40);
    MoneyMultiplier.graphics.endFill();  

    shape = new Shape();
    shape.graphics.beginFill(0x00FF00);
    shape.graphics.lineStyle(1);
    shape.graphics.drawCircle(0,0,15);
    shape.graphics.endFill();
    MoneyMultiplier.addChild(shape);
    shape.x = 0;
    shape.y = -3;

    var MoneyText = new TextField();
    MoneyText.text = "> $";
    MoneyText.selectable = false;
    MoneyText.mouseEnabled = false;
    MoneyMultiplier.addChild(MoneyText);
    MoneyText.x = -11;
    MoneyText.y = -11;


    MoneyMultiplier.isHover = true;
    MoneyMultiplier.buttonMode = true;
    MoneyMultiplier.addEventListener(MouseEvent.CLICK, onMoneyMultiplierClick);
    MoneyMultiplier.x=180;
    MoneyMultiplier.y=280;
    MoneyMultiplierLevel = new TextField();
    MoneyMultiplier.addChild(MoneyMultiplierLevel);
    MoneyMultiplierLevel.x = -25;MoneyMultiplierLevel.y=15;
    MoneyMultiplierLevel.mouseEnabled = false;
    MoneyMultiplierLevel.selectable = false;
    MoneyMultiplierLevel.autoSize = TextFieldAutoSize.LEFT;

    MoneyMultiplierMaxed = new TextField();
    MoneyMultiplier.addChild(MoneyMultiplierMaxed);
    MoneyMultiplierMaxed.x = -39;MoneyMultiplierMaxed.y=-10;
    MoneyMultiplierMaxed.mouseEnabled = false;
    MoneyMultiplierMaxed.selectable = false;
    MoneyMultiplierMaxed.autoSize = TextFieldAutoSize.LEFT;
    MoneyMultiplierMaxed.background = false;
    MoneyMultiplierMaxed.backgroundColor = 0xFFFFFF;


    ShotsFiredRate = new CustomSprite();
    ShotsFiredRate.graphics.lineStyle(1);
    ShotsFiredRate.graphics.beginFill(0xFFFFFF);
    ShotsFiredRate.graphics.drawCircle(0, 0,40);
    ShotsFiredRate.graphics.endFill();  

    shape = new Shape();
    shape.graphics.beginFill(0x000000);
    shape.graphics.lineStyle(1);
    shape.graphics.drawCircle(0,0,10);
    shape.graphics.endFill();
    ShotsFiredRate.addChild(shape);
    shape.x = -5;
    shape.y = -3;

    shape = new Shape();
    shape.graphics.beginFill(0x999999);
    shape.graphics.lineStyle(1);
    shape.graphics.drawCircle(0,0,3);
    shape.graphics.endFill();
    ShotsFiredRate.addChild(shape);
    shape.x = -10;
    shape.y = -17;

    shape = new Shape();
    shape.graphics.beginFill(0x999999);
    shape.graphics.lineStyle(1);
    shape.graphics.drawCircle(0,0,3);
    shape.graphics.endFill();
    ShotsFiredRate.addChild(shape);
    shape.x = 5;
    shape.y = 10;

    shape = new Shape();
    shape.graphics.beginFill(0x999999);
    shape.graphics.lineStyle(1);
    shape.graphics.drawCircle(0,0,3);
    shape.graphics.endFill();
    ShotsFiredRate.addChild(shape);
    shape.x =  5;
    shape.y = -17;

    shape = new Shape();
    shape.graphics.beginFill(0x999999);
    shape.graphics.lineStyle(1);
    shape.graphics.drawCircle(0,0,3);
    shape.graphics.endFill();
    ShotsFiredRate.addChild(shape);
    shape.x =  15;
    shape.y = -13;

    shape = new Shape();
    shape.graphics.beginFill(0x999999);
    shape.graphics.lineStyle(1);
    shape.graphics.drawCircle(0,0,3);
    shape.graphics.endFill();
    ShotsFiredRate.addChild(shape);
    shape.x =  20;
    shape.y = -10;
    shape = new Shape();
    shape.graphics.beginFill(0x999999);
    shape.graphics.lineStyle(1);
    shape.graphics.drawCircle(0,0,3);
    shape.graphics.endFill();
    ShotsFiredRate.addChild(shape);
    shape.x =  25;
    shape.y =  0;

    ShotsFiredRate.isHover = true;
    ShotsFiredRate.buttonMode = true;
    ShotsFiredRate.addEventListener(MouseEvent.CLICK, onShotsFiredRateClick);
    ShotsFiredRate.x=280;
    ShotsFiredRate.y=100;
    ShotsFiredRateLevel = new TextField();
    ShotsFiredRate.addChild(ShotsFiredRateLevel);
    ShotsFiredRateLevel.x = -25;ShotsFiredRateLevel.y=15;
    ShotsFiredRateLevel.mouseEnabled = false;
    ShotsFiredRateLevel.selectable = false;
    ShotsFiredRateLevel.autoSize = TextFieldAutoSize.LEFT;

    ShotsFiredRateMaxed = new TextField();
    ShotsFiredRate.addChild(ShotsFiredRateMaxed);
    ShotsFiredRateMaxed.x = -39;ShotsFiredRateMaxed.y=-10;
    ShotsFiredRateMaxed.mouseEnabled = false;
    ShotsFiredRateMaxed.selectable = false;
    ShotsFiredRateMaxed.autoSize = TextFieldAutoSize.LEFT;
    ShotsFiredRateMaxed.background = false;
    ShotsFiredRateMaxed.backgroundColor = 0xFFFFFF;




    //Add Upgrade Buttons
    //Row 3
    addChild(ShotsFiredRate);
    //Row 2
    addChild(MoneyMultiplier);
    addChild(PowerUpFadeRate);
    addChild(InvincibilityLength);
    //Row 1
    addChild(PowerUpAppearance);
    addChild(Health);
    addChild(SmallerShip);
    


    BackButton  = new Sprite();    
    BackButton.graphics.lineStyle(1);
    BackButton.graphics.beginFill(0xFFFFFF);
    BackButton.graphics.drawCircle(0, 0, 200);
    BackButton.x = 740;
    BackButton.y = 417;
    BackButton.buttonMode = true;
    BackButton.addEventListener(MouseEvent.CLICK, onBackButtonClick);
    addChild(BackButton);
    var ButtonText = new TextField();
    ButtonText.htmlText = "<font size ='32'>Back</font>";
    ButtonText.selectable =  false;
    ButtonText.x = -100; ButtonText.y = -80;
    BackButton.addChild(ButtonText);
    addEventListener(Event.ADDED_TO_STAGE, addedToStagHandler);
}

private function drawUpgradeLevelInfo()
{

  Money.htmlText = "<font size=\"24\" >Total Cash: $" + gameData.Money+"</font>";

  var ShipSizeUpgradeLevel:Int = UpgradesInfo.ShipSizeUpgrade.getLevel(gameData.ShipSize);
  if(UpgradesInfo.ShipSizeUpgrade.isMaxed(ShipSizeUpgradeLevel))
  {
  SmallerShipMaxed.background = true;
  SmallerShipMaxed.htmlText="<font size=\"14\" >     Maxed    </font>"; 

  //Disabled
  SmallerShip.graphics.clear();
  SmallerShip.graphics.lineStyle(1,0xFFFFFF);
  SmallerShip.graphics.beginFill(0x4F4F4F);
  SmallerShip.graphics.drawCircle(0, 0,40);
  SmallerShip.graphics.endFill();
  SmallerShip.HoverText="<u>Smaller Ship - (Maxed)</u>\nThis will decrease the Size of your ship";
  }
  else
  {
  SmallerShipMaxed.htmlText="";
  SmallerShipSize.htmlText="<font size=\"12\">" + gameData.ShipSize + "</font>";
  SmallerShip.HoverText="<u>Smaller Ship - $"+ UpgradesInfo.ShipSizeUpgrade.getNextLevelCost(ShipSizeUpgradeLevel)  + "</u>\nThis will decrease the Size of your ship";
  }

var HealthUpgradeLevel:Int = UpgradesInfo.HealthUpgrade.getLevel(gameData.MaxHealth);
  if(UpgradesInfo.HealthUpgrade.isMaxed(HealthUpgradeLevel))
  {
  HealthMaxed.background = true;
  HealthMaxed.htmlText="<font size=\"14\" >     Maxed    </font>"; 

  //Disabled
  Health.graphics.clear();
  Health.graphics.lineStyle(1,0xFFFFFF);
  Health.graphics.beginFill(0x4F4F4F);
  Health.graphics.drawCircle(0, 0,40);
  Health.graphics.endFill();
  Health.HoverText="<u>Health - (Maxed)</u>\nThis will add Health to your ship";
  }
  else
  {
  HealthMaxed.htmlText="";
  HealthAmount.htmlText="<font size=\"12\">" + gameData.MaxHealth + "</font>";
  Health.HoverText="<u>Health - $"+ UpgradesInfo.HealthUpgrade.getNextLevelCost(HealthUpgradeLevel)  + "</u>\nThis will add Health to your ship";
  }

//PowerUpAppearance
var PowerUpAppearanceUpgradeLevel:Int = UpgradesInfo.PowerUpAppearanceUpgrade.getLevel(gameData.PowerUpAppearanceRate);
  if(UpgradesInfo.PowerUpAppearanceUpgrade.isMaxed(PowerUpAppearanceUpgradeLevel))
  {
  PowerUpAppearanceMaxed.background = true;
  PowerUpAppearanceMaxed.htmlText="<font size=\"14\" >     Maxed    </font>"; 

  //Disabled
  PowerUpAppearance.graphics.clear();
  PowerUpAppearance.graphics.lineStyle(1,0xFFFFFF);
  PowerUpAppearance.graphics.beginFill(0x4F4F4F);
  PowerUpAppearance.graphics.drawCircle(0, 0,40);
  PowerUpAppearance.graphics.endFill();
  PowerUpAppearance.HoverText="<u>Power Up Appearance - (Maxed)</u>\nThis will increase the amount of Power Ups that appear on screen when an enemy is defeated";
  }
  else
  {
  PowerUpAppearanceMaxed.htmlText="";
  PowerUpAppearanceAmount.htmlText="<font size=\"12\">" + gameData.PowerUpAppearanceRate + "</font>";
  PowerUpAppearance.HoverText="<u>Power Up Appearance - $"+ UpgradesInfo.PowerUpAppearanceUpgrade.getNextLevelCost(PowerUpAppearanceUpgradeLevel)  + "</u>\nThis will increase the amount of Power Ups that appear on screen when an enemy is defeated";
  }

//InvincibilityLength
  var InvincibilityLengthUpgradeLevel:Int = UpgradesInfo.InvincibilityLengthUpgrade.getLevel(gameData.InvincibilityLength);

  if(gameData.Difficulty >= 3)
  {
    if(UpgradesInfo.InvincibilityLengthUpgrade.isMaxed(InvincibilityLengthUpgradeLevel))
    {
    InvincibilityLengthMaxed.background = true;
    InvincibilityLengthMaxed.htmlText="<font size=\"14\" >     Maxed    </font>"; 

    //Disabled
    InvincibilityLength.graphics.clear();
    InvincibilityLength.graphics.lineStyle(1,0xFFFFFF);
    InvincibilityLength.graphics.beginFill(0x4F4F4F);
    InvincibilityLength.graphics.drawCircle(0, 0,40);
    InvincibilityLength.graphics.endFill();
    InvincibilityLength.HoverText="<u>Invincibility Length - (Not Avalible)</u>\nIncreases the amount of time the Invincibility Power Ups remain active.";
    }
  }
  else
  {
    if(UpgradesInfo.InvincibilityLengthUpgrade.isMaxed(InvincibilityLengthUpgradeLevel))
    {
    InvincibilityLengthMaxed.background = true;
    InvincibilityLengthMaxed.htmlText="<font size=\"14\" >     Maxed    </font>"; 

    //Disabled
    InvincibilityLength.graphics.clear();
    InvincibilityLength.graphics.lineStyle(1,0xFFFFFF);
    InvincibilityLength.graphics.beginFill(0x4F4F4F);
    InvincibilityLength.graphics.drawCircle(0, 0,40);
    InvincibilityLength.graphics.endFill();
    InvincibilityLength.HoverText="<u>Invincibility Length - (Maxed)</u>\nIncreases the amount of time the Invincibility Power Ups remain active.";
    }
    else
    {
    InvincibilityLengthMaxed.htmlText="";
    InvincibilityLengthLevel.htmlText="<font size=\"12\">" + gameData.InvincibilityLength + "</font>";
    InvincibilityLength.HoverText="<u>Invincibility Length - $"+ UpgradesInfo.InvincibilityLengthUpgrade.getNextLevelCost(InvincibilityLengthUpgradeLevel)  + "</u>\nIncreases the amount of time the Invincibility Power Ups remain active.";
    }
  }
//PowerUpFadeRate
 var PowerUpFadeRateUpgradeLevel:Int = UpgradesInfo.PowerUpFadeRateUpgrade.getLevel(gameData.PowerUpFadeRate);
  if(UpgradesInfo.PowerUpFadeRateUpgrade.isMaxed(PowerUpFadeRateUpgradeLevel))
  {
  PowerUpFadeRateMaxed.background = true;
  PowerUpFadeRateMaxed.htmlText="<font size=\"14\" >     Maxed    </font>"; 

  //Disabled
  PowerUpFadeRate.graphics.clear();
  PowerUpFadeRate.graphics.lineStyle(1,0xFFFFFF);
  PowerUpFadeRate.graphics.beginFill(0x4F4F4F);
  PowerUpFadeRate.graphics.drawCircle(0, 0,40);
  PowerUpFadeRate.graphics.endFill();
  PowerUpFadeRate.HoverText="<u>Power Up Fade Rate - (Maxed)</u>\nIncreases the amount of time the Power Ups remain on screen.";
  }
  else
  {
  PowerUpFadeRateMaxed.htmlText="";
  PowerUpFadeRateLevel.htmlText="<font size=\"12\">" + gameData.PowerUpFadeRate + "</font>";
  PowerUpFadeRate.HoverText="<u>Power Up Fade Rate - $"+ UpgradesInfo.PowerUpFadeRateUpgrade.getNextLevelCost(PowerUpFadeRateUpgradeLevel)  + "</u>\nIncreases the amount of time the Power Ups remain on screen.";
  }

//MoneyMultiplier
  var MoneyMultiplierUpgradeLevel:Int = UpgradesInfo.MoneyMultiplierUpgrade.getLevel(gameData.MoneyMultiplier);
  if(UpgradesInfo.MoneyMultiplierUpgrade.isMaxed(MoneyMultiplierUpgradeLevel))
  {
  MoneyMultiplierMaxed.background = true;
  MoneyMultiplierMaxed.htmlText="<font size=\"14\" >     Maxed    </font>"; 

  //Disabled
  MoneyMultiplier.graphics.clear();
  MoneyMultiplier.graphics.lineStyle(1,0xFFFFFF);
  MoneyMultiplier.graphics.beginFill(0x4F4F4F);
  MoneyMultiplier.graphics.drawCircle(0, 0,40);
  MoneyMultiplier.graphics.endFill();
  MoneyMultiplier.HoverText="<u>Money Multiplier - (Maxed)</u>\nIncreases the value of money power ups and bullets fired.";
  }
  else
  {
  MoneyMultiplierMaxed.htmlText="";
  MoneyMultiplierLevel.htmlText="<font size=\"12\">" + gameData.MoneyMultiplier + "</font>";
  MoneyMultiplier.HoverText="<u>Money Multiplier - $"+ UpgradesInfo.MoneyMultiplierUpgrade.getNextLevelCost(MoneyMultiplierUpgradeLevel)  + "</u>\nIncreases the value of money power ups and bullets fired.";
  }

  //ShotsFiredRate
  var ShotsFiredRateUpgradeLevel:Int = UpgradesInfo.ShotsFiredRateUpgrade.getLevel(gameData.ShotsFiredRate);
  if(UpgradesInfo.ShotsFiredRateUpgrade.isMaxed(ShotsFiredRateUpgradeLevel))
  {
  ShotsFiredRateMaxed.background = true;
  ShotsFiredRateMaxed.htmlText="<font size=\"14\" >     Maxed    </font>"; 

  //Disabled
  ShotsFiredRate.graphics.clear();
  ShotsFiredRate.graphics.lineStyle(1,0xFFFFFF);
  ShotsFiredRate.graphics.beginFill(0x4F4F4F);
  ShotsFiredRate.graphics.drawCircle(0, 0,40);
  ShotsFiredRate.graphics.endFill();
  ShotsFiredRate.HoverText="<u>Shots Per Frame - (Maxed)</u>\nIncreases the amount of shots are fired at once.";
  }
  else
  {
  ShotsFiredRateMaxed.htmlText="";
  ShotsFiredRateLevel.htmlText="<font size=\"12\">" + gameData.ShotsFiredRate + "</font>";
  ShotsFiredRate.HoverText="<u>Shots Per Frame - $"+ UpgradesInfo.ShotsFiredRateUpgrade.getNextLevelCost(ShotsFiredRateUpgradeLevel)  + "</u>\nIncreases the amount of shots are fired at once.";
  }

}

//assume GameData has been loaded before enter.
private function addedToStagHandler(event:Event)
{
UpgradesInfo = new Upgrades(gameData.Difficulty);
drawUpgradeLevelInfo();
}

private function onSmallerShipClick(event:MouseEvent) {
var ShipSizeUpgradeLevel:Int = UpgradesInfo.ShipSizeUpgrade.getLevel(gameData.ShipSize);
  if(!UpgradesInfo.ShipSizeUpgrade.isMaxed(ShipSizeUpgradeLevel))
  {
    var upgradeCost:Int = Std.parseInt(UpgradesInfo.ShipSizeUpgrade.getNextLevelCost(ShipSizeUpgradeLevel));
    if(gameData.Money >= upgradeCost)
      {
	gameData.Money -= upgradeCost;
	gameData.ShipSize = Std.int(UpgradesInfo.ShipSizeUpgrade.getNextLevelEffect(ShipSizeUpgradeLevel));
	SmallerShipSize.htmlText="<font size=\"12\">" + gameData.ShipSize + "</font>";
      }
  }
   drawUpgradeLevelInfo();
}

private function onHealthClick(event:MouseEvent) {
var HealthUpgradeLevel:Int = UpgradesInfo.HealthUpgrade.getLevel(gameData.MaxHealth);
  if(!UpgradesInfo.HealthUpgrade.isMaxed(HealthUpgradeLevel))
  {
    var upgradeCost:Int = Std.parseInt(UpgradesInfo.HealthUpgrade.getNextLevelCost(HealthUpgradeLevel));
    if(gameData.Money >= upgradeCost)
    {
      gameData.Money -= upgradeCost;
      gameData.MaxHealth = Std.int(UpgradesInfo.HealthUpgrade.getNextLevelEffect(HealthUpgradeLevel));
      HealthAmount.htmlText="<font size=\"12\">" + gameData.MaxHealth + "</font>";
    }
  }
   drawUpgradeLevelInfo();
}

private function onPowerUpAppearanceClick(event:MouseEvent) {
var PowerUpAppearanceUpgradeLevel:Int = UpgradesInfo.PowerUpAppearanceUpgrade.getLevel(gameData.PowerUpAppearanceRate);
  if(!UpgradesInfo.PowerUpAppearanceUpgrade.isMaxed(PowerUpAppearanceUpgradeLevel))
  {
    var upgradeCost:Int = Std.parseInt(UpgradesInfo.PowerUpAppearanceUpgrade.getNextLevelCost(PowerUpAppearanceUpgradeLevel));
    if(gameData.Money >= upgradeCost)
    {
      gameData.Money -= upgradeCost;
      gameData.PowerUpAppearanceRate = Std.int(UpgradesInfo.PowerUpAppearanceUpgrade.getNextLevelEffect(PowerUpAppearanceUpgradeLevel));
      PowerUpAppearanceAmount.htmlText="<font size=\"12\">" + gameData.PowerUpAppearanceRate + "</font>";
    }
  }
   drawUpgradeLevelInfo();
}


private function onInvincibilityLengthClick(event:MouseEvent) {
  
var InvincibilityLengthUpgradeLevel:Int = UpgradesInfo.InvincibilityLengthUpgrade.getLevel(gameData.InvincibilityLength);
  if(!UpgradesInfo.InvincibilityLengthUpgrade.isMaxed(InvincibilityLengthUpgradeLevel))
  {
    var upgradeCost:Int = Std.parseInt(UpgradesInfo.InvincibilityLengthUpgrade.getNextLevelCost(InvincibilityLengthUpgradeLevel));
    if(gameData.Money >= upgradeCost)
    {
      gameData.Money -= upgradeCost;
      gameData.InvincibilityLength = Std.int(UpgradesInfo.InvincibilityLengthUpgrade.getNextLevelEffect(InvincibilityLengthUpgradeLevel));
      InvincibilityLengthLevel.htmlText="<font size=\"12\">" + gameData.InvincibilityLength + "</font>"; 
    }
  }
   drawUpgradeLevelInfo();
}

private function onPowerUpFadeRateClick(event:MouseEvent) {
var PowerUpFadeRateUpgradeLevel:Int = UpgradesInfo.PowerUpFadeRateUpgrade.getLevel(gameData.PowerUpFadeRate);
  if(!UpgradesInfo.PowerUpFadeRateUpgrade.isMaxed(PowerUpFadeRateUpgradeLevel))
  {
    var upgradeCost:Int = Std.parseInt(UpgradesInfo.PowerUpFadeRateUpgrade.getNextLevelCost(PowerUpFadeRateUpgradeLevel));
    if(gameData.Money >= upgradeCost)
    {
      gameData.Money -= upgradeCost;
      gameData.PowerUpFadeRate = Std.int(UpgradesInfo.PowerUpFadeRateUpgrade.getNextLevelEffect(PowerUpFadeRateUpgradeLevel));
      PowerUpFadeRateLevel.htmlText="<font size=\"12\">" + gameData.PowerUpFadeRate + "</font>"; 
    }
  }
   drawUpgradeLevelInfo();
}

private function onMoneyMultiplierClick(event:MouseEvent) {
var MoneyMultiplierUpgradeLevel:Int = UpgradesInfo.MoneyMultiplierUpgrade.getLevel(gameData.MoneyMultiplier);
  if(!UpgradesInfo.MoneyMultiplierUpgrade.isMaxed(MoneyMultiplierUpgradeLevel))
  {
    var upgradeCost:Int = Std.parseInt(UpgradesInfo.MoneyMultiplierUpgrade.getNextLevelCost(MoneyMultiplierUpgradeLevel));
    if(gameData.Money >= upgradeCost)
      {gameData.Money -= upgradeCost;gameData.MoneyMultiplier = UpgradesInfo.MoneyMultiplierUpgrade.getNextLevelEffect(MoneyMultiplierUpgradeLevel); MoneyMultiplierLevel.htmlText="<font size=\"12\">" + gameData.MoneyMultiplier + "</font>"; }
  }
   drawUpgradeLevelInfo();
}

private function onShotsFiredRateClick(event:MouseEvent) {
var ShotsFiredRateUpgradeLevel:Int = UpgradesInfo.ShotsFiredRateUpgrade.getLevel(gameData.ShotsFiredRate);
  if(!UpgradesInfo.ShotsFiredRateUpgrade.isMaxed(ShotsFiredRateUpgradeLevel))
  {
    var upgradeCost:Int = Std.parseInt(UpgradesInfo.ShotsFiredRateUpgrade.getNextLevelCost(ShotsFiredRateUpgradeLevel));
    if(gameData.Money >= upgradeCost)
    {
      gameData.Money -= upgradeCost;
      gameData.ShotsFiredRate = Std.int(UpgradesInfo.ShotsFiredRateUpgrade.getNextLevelEffect(ShotsFiredRateUpgradeLevel));
      ShotsFiredRateLevel.htmlText="<font size=\"12\">" + gameData.ShotsFiredRate + "</font>"; }
  }
   drawUpgradeLevelInfo();
}

private function onBackButtonClick(event:MouseEvent) {
    Back();
}

  public function Back()
  {
  dispatchEvent(new UpgradePageEvent("Back"));
  }
}